<template>
  <div class="app-container" v-loading="loading">
    <el-row :gutter="10">
      <el-col :span="19">
        <!--信审信息-->
        <credit-info :detail-info="detailInfo"></credit-info>
        <!--申报人信息-->
        <apply-person-info :reporter-info="detailInfo"></apply-person-info>
        <!--客户信息-->
        <customer-info :customer-info="detailInfo.customer" :application-no="detailInfo.applicationNo"></customer-info>
        <!--订单信息-->
        <order-info :order-info="detailInfo.order" :cars="detailInfo.cars" :clueClassification="clueClassification"></order-info>
        <!--担保人信息(有担保人信息就展示,没有就不展示)-->
        <guarantor-info v-if="detailInfo.personal||detailInfo.enterprise" :personal="detailInfo.personal" :enterprise="detailInfo.enterprise"></guarantor-info>
        <!--买受人信息(租购的时候显示)-->
        <vendee-info v-if="detailInfo.vendee" :vendee-info="detailInfo.vendee"></vendee-info>

        <!--信审资料(需要信审显示)-->
        <letter-review-files-info v-if="detailInfo.needCreditReview==2" :detail-info="detailInfo"></letter-review-files-info>

        <!--资料补充记录|准入结果-->
        <template v-if="detailInfo.creditReviewStatus==2">
          <!--资料补充记录-->
          <additional-record-info v-if="detailInfo.fileAddRecordVo && detailInfo.fileAddRecordVo.length>0" :addData="detailInfo.fileAddRecordVo"></additional-record-info>
          <!--准入结果-->
          <admission-results-form v-if="checkPermi(['letterReview:letterReview:access'])&&detailInfo.isRecipient" ref="admissionResultsForm"
                                  :customerNo="detailInfo.customer.customerNo"></admission-results-form>
        </template>
        <template v-else-if="detailInfo.needCreditReview==2">
          <!--资料补充记录-->
          <additional-record-info v-if="detailInfo.fileAddRecordVo && detailInfo.fileAddRecordVo.length>0" :addData="detailInfo.fileAddRecordVo"></additional-record-info>
        </template>
        <!--信审批复建议回显(批复建议审批中|审批拒绝|审批通过)-->
        <!--信审批复建议回显(批复建议审批中|审批拒绝|审批通过)-->
        <reply-suggestions-info v-if="detailInfo.creditReviewStatus>=4 && detailInfo.creditReviewStatus<=9 && detailInfo.needCreditReview==2"
                                :records="detailInfo.records"></reply-suggestions-info>
        <!--信审批复建议表单(待提交批复建议|批复建议被拒绝)-->
        <reply-suggestion-form ref="replySuggestionForm"
                               v-if="(detailInfo.creditReviewStatus==3||detailInfo.creditReviewStatus==5)&&checkPermi(['letterReview:letterReview:commitSuggestion'])&&detailInfo.isRecipient"
                               :results="detailInfo.results" :customerNo="detailInfo.customer.customerNo"></reply-suggestion-form>

        <template v-if="detailInfo.creditReviewStatus==6&&checkPermi(['letterReview:letterReview:commitResult'])&&detailInfo.isRecipient">
          <CRCOpinionForm v-if="detailInfo.needCrc==1" ref="crcOpinionForm"></CRCOpinionForm>
          <!--提交信审结果-->
          <letter-review-result-form ref="letterReviewResultForm" :results="detailInfo.results"></letter-review-result-form>
        </template>

        <template v-if="detailInfo.creditReviewStatus>=7&&detailInfo.creditReviewStatus<=9">
          <!--CRC意见回显-->
          <CRCOpinionInfo v-if="detailInfo.needCrc==1" :detail-info="detailInfo"></CRCOpinionInfo>
          <!--信审结果回显-->
          <letter-review-result-info :results="detailInfo.results" :needCreditReview="detailInfo.needCreditReview"
                                     :cancle-order-info="detailInfo.cancellationInstructions"
                                     :application-no="detailInfo.applicationNo"></letter-review-result-info>
        </template>
        <!--不予准入回显-->
        <admission-results-info v-if="detailInfo.creditReviewStatus==10" :reporterInfo="detailInfo"></admission-results-info>
        <!--撤单说明-->
        <cancle-order-info v-if="detailInfo.creditReviewStatus==11" :cancle-order-info="detailInfo.cancellationInstructions"></cancle-order-info>
        <!--免信审记录-->
        <no-credit-review-record v-if="detailInfo.needCreditReview==1" :no-credit-records="detailInfo.crRuleItem"></no-credit-review-record>

        <div class="card-wrap flex justify-end card-padding">
          <el-button @click="back">返回</el-button>
          <!--提交准入结果-->
          <el-button v-if="detailInfo.creditReviewStatus==2&&checkPermi(['letterReview:letterReview:access'])&&detailInfo.isRecipient" type="primary" v-loading="submitLoading"
                     @click="submitForm(1)">提交
          </el-button>
          <!--提交批复建议-->
          <el-button v-if="(detailInfo.creditReviewStatus==3||detailInfo.creditReviewStatus==5)&&checkPermi(['letterReview:letterReview:commitSuggestion'])&&detailInfo.isRecipient"
                     type="primary" v-loading="submitLoading" @click="submitForm(2)">提交批复建议
          </el-button>
          <!--提交信审结果-->
          <el-button v-if="detailInfo.creditReviewStatus==6&&checkPermi(['letterReview:letterReview:commitResult'])&&detailInfo.isRecipient" v-loading="submitLoading"
                     type="primary" @click="submitForm(3)">提交结果
          </el-button>
        </div>

      </el-col>
      <el-col :span="5">
        <!--信审申请记录-->
        <apply-record v-if="detailInfo.logs && detailInfo.logs.length>0" :logs="detailInfo.logs"></apply-record>
      </el-col>
    </el-row>


  </div>
</template>

<script>
import CreditInfo from "@/views/letterReview/letterReview/components/CreditInfo.vue";
import ApplyPersonInfo from "@/views/letterReview/letterReview/components/ApplyPersonInfo.vue";
import CustomerInfo from "@/views/letterReview/letterReview/components/CustomerInfo.vue";
import OrderInfo from "@/views/letterReview/letterReview/components/OrderInfo.vue";
import GuarantorInfo from "@/views/letterReview/letterReview/components/GuarantorInfo.vue";
import AdditionalRecordInfo from "@/views/letterReview/letterReview/components/AdditionalRecordInfo.vue";
import AdmissionResultsForm from "@/views/letterReview/letterReview/components/AdmissionResultsForm.vue";
import AdmissionResultsInfo from "@/views/letterReview/letterReview/components/AdmissionResultsInfo.vue";
import LetterReviewFilesInfo from "@/views/letterReview/letterReview/components/LetterReviewFilesInfo.vue";
import ReplySuggestionForm from "@/views/letterReview/letterReview/components/ReplySuggestionForm.vue";
import ReplySuggestionsInfo from "@/views/letterReview/letterReview/components/ReplySuggestionsInfo.vue";
import CRCOpinionForm from "@/views/letterReview/letterReview/components/CRCOpinionForm.vue";
import CRCOpinionInfo from "@/views/letterReview/letterReview/components/CRCOpinionInfo.vue";
import LetterReviewResultForm from "@/views/letterReview/letterReview/components/LetterReviewResultForm.vue";
import LetterReviewResultInfo from "@/views/letterReview/letterReview/components/LetterReviewResultInfo.vue";
import ApplyRecord from "@/views/letterReview/letterReview/components/ApplyRecord.vue";
import ApprovalInfo from "@/views/letterReview/letterReview/components/ApprovalInfo.vue";
import NoCreditReviewRecord from "@/views/letterReview/letterReview/components/NoCreditReviewRecord.vue";
import CancleOrderInfo from "@/views/letterReview/letterReview/components/CancleOrderInfo.vue";
import VendeeInfo from "@/views/letterReview/letterReview/components/VendeeInfo.vue";

import FilePreview from "@/components/FilePreview/index.vue";

import * as letterReviewApi from '@/api/letterReview/letterReview'

import {checkPermi} from "@/utils/permission";


export default {
  name: 'LetterReviewDetail',
  components: {
    CreditInfo,
    VendeeInfo,
    ApplyPersonInfo,
    CustomerInfo,
    OrderInfo,
    GuarantorInfo,
    AdditionalRecordInfo,
    AdmissionResultsForm,
    AdmissionResultsInfo,
    LetterReviewFilesInfo,
    ReplySuggestionForm,
    ReplySuggestionsInfo,
    CRCOpinionForm,
    CRCOpinionInfo,
    LetterReviewResultForm,
    LetterReviewResultInfo,
    ApplyRecord,
    ApprovalInfo,
    NoCreditReviewRecord,
    CancleOrderInfo
  },
  data() {
    return {
      loading: false,
      submitLoading: false,
      applicationNo: null,
      id: null,
      detailInfo: {},
      clueClassification: null,

    };
  },
  created() {
    this.id = this.$route.query?.id;
    this.applicationNo = this.$route.query?.applicationNo;
    this.getLetterReviewDetail();
  },
  mounted() {

  },
  methods: {
    checkPermi,
    // 获取信审详情信息
    getLetterReviewDetail() {
      this.loading = true
      letterReviewApi.creditReviewDetail(this.applicationNo).then((res) => {
        this.detailInfo = res.data
        this.clueClassification = this.detailInfo?.customer?.clueClassification;
      }).finally(() => {
        this.loading = false
      })
    },
    submitForm(type) {
      switch (type) {
        case 1://提交准入结果
          this.$refs?.admissionResultsForm?.saveSubmit()
          break
        case 2://提交批复建议
          this.$refs?.replySuggestionForm?.saveSubmit()
          break
        case 3://提交信审结果
          this.commitCreditResult()
          break
      }
    },
    commitCreditResult() {
      //获取CRC提交信息
      const crcForm = this.$refs?.crcOpinionForm?.saveSubmit()
      // 获取信审结果提交信息
      const letterResultForm = this.$refs?.letterReviewResultForm?.saveSubmit()
      if (letterResultForm == null) {
        return
      }
      //最终提交的form
      const form = {
        ...crcForm,
        records: {
          ...letterResultForm
        },
        id: this.id
      }
      console.log(crcForm, letterResultForm, form)
      this.submitLoading = true
      setTimeout(() => {
        letterReviewApi.submitCreditReviewResults(form).then(res => {
          if (res.code == 200) {
            this.back()
          }
        }).finally(() => {
          this.submitLoading = false
        })
      }, 300)

    },
    back() {
      this.$router.back()
      this.$store.dispatch('tagsView/delView', this.$route)
    }

  },

}

</script>


<style lang="scss" scoped>

</style>
